package com.labichaoka.chaoka.ui.baseinfo.information;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.labichaoka.chaoka.R;
import com.labichaoka.chaoka.base.BaseActivity;
import com.labichaoka.chaoka.base.GlobalParams;
import com.labichaoka.chaoka.entity.CreditCardResponse;
import com.labichaoka.chaoka.entity.IDCardResponse;
import com.labichaoka.chaoka.entity.VerifyInfoListResponse;
import com.labichaoka.chaoka.ui.bank.bind.BankBindActivity;
import com.labichaoka.chaoka.ui.baseinfo.person.PersonInfoActivity;
import com.labichaoka.chaoka.ui.credit.CalculateActivity;
import com.labichaoka.chaoka.ui.web.WebViewActivity;
import com.labichaoka.chaoka.utils.SharedPreferencesManager;
import com.labichaoka.chaoka.utils.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class InformationAuthenticationActivity extends BaseActivity implements InformationAuthenticationView {
    @BindView(R.id.next1)
    TextView next1;
    @BindView(R.id.next2)
    TextView next2;
    @BindView(R.id.next3)
    TextView next3;
    @BindView(R.id.next4)
    TextView next4;
    @BindView(R.id.person)
    TextView person;
    @BindView(R.id.operator)
    TextView operatorT;
    @BindView(R.id.bank)
    TextView bank;
    @BindView(R.id.credit_text)
    TextView creditText;
    @BindView(R.id.credit)
    TextView credit;
    @BindView(R.id.step4)
    TextView step4;
    @BindView(R.id.personInfoLayout)
    LinearLayout personInfoLayout;
    @BindView(R.id.operatorLayout)
    LinearLayout operatorLayout;
    @BindView(R.id.bankLayout)
    LinearLayout bankLayout;
    @BindView(R.id.creditLayout)
    LinearLayout creditLayout;

    private InformationAuthenticationPresenter presenter;
    private String bankCardAuthState;
    private String operatorAuthState;
    private String perInfoAuthState;
    private String creditCardState;
    private String creditUrl;
    private String creditStatus;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_information_authentication;
    }

    @Override
    protected void initView() {
        next1.setTypeface(iconfont);
        next2.setTypeface(iconfont);
        next3.setTypeface(iconfont);
        next4.setTypeface(iconfont);
    }

    @Override
    protected void initData() {
        presenter = new InformationAuthenticationPresenterImpl(this, new InformationAuthenticationInteractorImpl());
    }

    @OnClick({R.id.personInfoLayout, R.id.bankLayout, R.id.operatorLayout, R.id.creditLayout})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.personInfoLayout:
                if (bankCardAuthState.equals("0")) {
                    ToastUtils.show("请先完善银行卡信息");

                } else if (operatorAuthState.equals("0")) {
                    ToastUtils.show("请先完善运营商授权");
                } else if (creditStatus.equals("01")) {
                    if (creditCardState.equals("0")||creditCardState.equals("3")) {
                        ToastUtils.show("请先完成信用评估");
                    }else{
                        presenter.behavior(GlobalParams.CLICK_USERINFO);
                        gotoActivity(mContext, PersonInfoActivity.class, null);
                    }
                } else {
                    presenter.behavior(GlobalParams.CLICK_USERINFO);
                    gotoActivity(mContext, PersonInfoActivity.class, null);
                }
                break;
            case R.id.operatorLayout:
                if (bankCardAuthState.equals("0")) {
                    ToastUtils.show("请先完善银行卡信息");
                } else {
                    presenter.behavior(GlobalParams.CLICK_OPERATOR);
                    //先获取运营商需要传的参数
                    presenter.getIdName();
                }
                break;

            case R.id.bankLayout:
                presenter.behavior(GlobalParams.CLICK_BIND_BANKCARD);
                gotoActivity(mContext, BankBindActivity.class, null);
                break;
            case R.id.creditLayout:
                if (bankCardAuthState.equals("0")) {
                    ToastUtils.show("请先完善银行卡信息");

                } else if (operatorAuthState.equals("0")) {
                    ToastUtils.show("请先完善运营商授权");
                } else {
                    presenter.behavior(GlobalParams.APP_CREDITRAT_CLICK);
                    Bundle bundle = new Bundle();
                    bundle.putString("type", "10");
                    bundle.putString("url", creditUrl);
                    gotoActivity(mContext, WebViewActivity.class, bundle);
                }

                break;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void showProgress() {
        showLoadingProgress();
    }

    @Override
    public void hideProgress() {
        hideLoadingProgress();
    }

    @Override
    public void getVerifyInfoListCallback(VerifyInfoListResponse response) {
        if (response.getData() == null) {
            ToastUtils.show("获取数据异常，请稍后重试！");
            return;
        }
        bankCardAuthState = response.getData().getBankCardAuthState();
        operatorAuthState = response.getData().getOperatorAuthState();
        perInfoAuthState = response.getData().getPerInfoAuthState();
        creditCardState = response.getData().getCreditCardState();

        if (TextUtils.isEmpty(bankCardAuthState) || TextUtils.isEmpty(operatorAuthState) ||
                TextUtils.isEmpty(perInfoAuthState) || TextUtils.isEmpty(creditCardState)) {
            ToastUtils.show("获取数据异常，请稍后重试！");
            return;
        }

        if (TextUtils.isEmpty(creditStatus)) {
            ToastUtils.show("获取数据异常，请稍后重试！");
            return;
        }

        if (creditStatus.equals("01")) {
            //信用必填
            if (bankCardAuthState.equals("1") && operatorAuthState.equals("1") &&
                    perInfoAuthState.equals("1") && creditCardState.equals("1")) {
                gotoActivity(mContext, CalculateActivity.class, null);
                finish();
            }
        } else {
            if (bankCardAuthState.equals("1") && operatorAuthState.equals("1") &&
                    perInfoAuthState.equals("1")) {
                gotoActivity(mContext, CalculateActivity.class, null);
                finish();
            }
        }

        if (creditCardState.equals("0")) {
            credit.setText("去评估");
            next3.setVisibility(View.VISIBLE);
            creditLayout.setEnabled(true);
        } else if (creditCardState.equals("1")) {
            credit.setText("已评估");
            next3.setVisibility(View.GONE);
            creditLayout.setEnabled(false);
            credit.setTextColor(getResources().getColor(R.color.font_909090));

        }

        setUI(perInfoAuthState, personInfoLayout, person, next4);
        setUI(operatorAuthState, operatorLayout, operatorT, next2);
        setUI(bankCardAuthState, bankLayout, bank, next1);
    }

    @Override
    public void getIdNameCallBack(IDCardResponse response) {
        Map<String, String> map = new HashMap<>();
        map.put("sourceUserId", response.getData().getPid());
        map.put("certId", response.getData().getIdcard());
        map.put("realName", response.getData().getRealName());
        map.put("sysSourceId", "LB");

        String phone = SharedPreferencesManager.getInstance().getString("phone", "");

        Map<String, String> map2 = new HashMap<>();
        map2.put("account", phone);
        map2.put("real_name", response.getData().getRealName());
        map2.put("id_card", response.getData().getIdcard());
        map2.put("user_id", response.getData().getPid());

        String jsonStr = new Gson().toJson(map);
        String loginParam = new Gson().toJson(map2);
        Bundle bundle = new Bundle();
        bundle.putString("extdata", jsonStr);
        bundle.putString("loginParams", loginParam);
        bundle.putString("type", "1");
        gotoActivity(mContext, WebViewActivity.class, bundle);
    }

    @Override
    public void creditCardSwitchCallback(CreditCardResponse response) {

        if (response.getData() == null) {
            ToastUtils.show("获取数据异常，请稍后重试！");
            return;
        }
        creditUrl = response.getData().getH5Url();
        creditStatus = response.getData().getStatus();
        if (creditStatus.equals("00")) {
            //信用卡栏不展示
            step4.setText("3");
        } else if (creditStatus.equals("01")) {
            //展示必填
            credit.setTextColor(getResources().getColor(R.color.font_ff4340));
            creditText.setText("信用评估(必填)");
            creditLayout.setVisibility(View.VISIBLE);
            step4.setText("4");
        } else if (creditStatus.equals("02")) {
            //展示非必填
            credit.setTextColor(getResources().getColor(R.color.font_ff4340));
            creditText.setText("信用评估(选填)");
            creditLayout.setVisibility(View.VISIBLE);
            step4.setText("4");
        }

        presenter.getVerifyInfoList();
    }

    public void setUI(String status, LinearLayout layout, TextView tv, TextView next) {
        if (!TextUtils.isEmpty(status) && status.equals("1")) {
            tv.setText("已认证");
            layout.setEnabled(false);
            next.setVisibility(View.GONE);
            tv.setTextColor(getResources().getColor(R.color.font_909090));
        } else {
            tv.setText("去完善");
            layout.setEnabled(true);
            next.setVisibility(View.VISIBLE);
            tv.setTextColor(getResources().getColor(R.color.font_ff4340));
        }

    }

    @Override
    protected void onRestart() {
        super.onRestart();

    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.creditCardSwitch();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }
}
